library(tidyverse)
library(fastDummies)
library(kableExtra)
options(knitr.kable.NA = "")
library(Hmisc)

outputs_path <- "../../outputs/replication/"

source("functions_descriptives.R")

ex <- readRDS("../../data/replication/experiment_data_tidied.rds")

# check how many switched to no party identity for r and r response
had_pid_w20 <- ex %>% filter(party_id_gov_op_w20 != "Non-partisan")
table(had_pid_w20$party_id_gov_op_w21)
# 294 switched to being non-partisans

with_dummies_ex <- ex %>%
  dummy_cols(select_columns = c("age_w21",
                                "class_w21",
                                "education_w21",
                                "party_id_gov_op_w21")) %>%
  mutate(female_w21 = case_when(
    male_w21 == 0 ~ 1,
    male_w21 == 1 ~ 0,
    is.na(male_w21) ~ NA_real_,
    TRUE ~ NA_real_)) %>%
  mutate(not_white_british_w21 = case_when(
    white_british_w21 == 0 ~ 1,
    white_british_w21 == 1 ~ 0,
    is.na(white_british_w21) ~ NA_real_,
    TRUE ~ NA_real_)) 

con <- with_dummies_ex %>% filter(treatment_w21 == "control")
pos <- with_dummies_ex %>% filter(treatment_w21 == "positive")
neg <- with_dummies_ex %>% filter(treatment_w21 == "negative")

demographic_variables <- tibble(variable = "male_w21",
                          neat_variable_name = "Male") %>%
  add_row(variable = "female_w21",
          neat_variable_name = "Female") %>%
  add_row(variable = "white_british_w21",
          neat_variable_name = "White British") %>%
  add_row(variable = "not_white_british_w21",
          neat_variable_name = "Not White British") %>%
  add_row(variable = "age_w21_18to29",
          neat_variable_name = "Age: 18 to 29") %>%
  add_row(variable = "age_w21_30to44",
          neat_variable_name = "Age: 30 to 44") %>%
  add_row(variable = "age_w21_45to59",
          neat_variable_name = "Age: 45 to 59") %>%
  add_row(variable = "age_w21_60plus",
          neat_variable_name = "Age: 60 plus") %>%
  add_row(variable = "class_w21_Higher Managerial",
          neat_variable_name =  "Class: Higher Managerial") %>%
  add_row(variable = "class_w21_Lower Professional",
          neat_variable_name = "Class: Lower Professional") %>%
  add_row(variable = "class_w21_Intermediate Occupations",
          neat_variable_name = "Class: Intermediate Occupations") %>%
  add_row(variable = "class_w21_Small Employers",
          neat_variable_name = "Class: Small Employers") %>%
  add_row(variable = "class_w21_Lower Supervisory",
          neat_variable_name = "Class: Lower Supervisory") %>%
  add_row(variable = "class_w21_Semi-routine Occupations",
          neat_variable_name = "Class: Semi-routine Occupations") %>%
  add_row(variable = "class_w21_Routine Occupations",
          neat_variable_name = "Class: Routine Occupations") %>%
  add_row(variable = "education_w21_Undergrad or higher",
          neat_variable_name = "Education: Undergrad or higher") %>%
  add_row(variable = "education_w21_A-level",
          neat_variable_name = "Education: A-level") %>%
  add_row(variable = "education_w21_GCSE",
          neat_variable_name = "Education: GCSE") %>%
  add_row(variable = "education_w21_Below GCSE",
          neat_variable_name = "Education: Below GCSE") %>%
  add_row(variable = "education_w21_No qualifications",
          neat_variable_name = "Education: No qualifications") %>%
  add_row(variable = "party_id_gov_op_w21_Opposition partisan",
          neat_variable_name = "Party Id: Opposition partisan") %>%
  add_row(variable = "party_id_gov_op_w21_Non-partisan",
          neat_variable_name = "Party Id: Non-partisan") %>%
  add_row(variable = "party_id_gov_op_w21_Government partisan",
          neat_variable_name = "Party Id: Government partisan")

with_weights_descriptive_table <- bind_rows(map(demographic_variables$variable, 
    weighted_treatment_group_demographics,
    df = with_dummies_ex)) %>%
  left_join(demographic_variables, by = "variable") %>%
  select(-variable) %>%
  rename(Variable = neat_variable_name) %>%
  relocate(Variable)

repeated_names <- c("All", "C", "+ve", "-ve")

kabled_with_weights_descriptive_table <- kable(with_weights_descriptive_table,
    col.names = c("Variable", repeated_names, repeated_names, repeated_names),
    format = "latex", 
    booktabs = TRUE, 
    caption = "Sample descriptives (wave 21 weights applied)", 
    label = " demographics_weighted",
    linesep = "",
    align = "lcccccccccccc") %>%
  kable_styling(latex_options = c("hold_position", "scale_down")) %>%
  add_header_above(c(" " = 1,
                     "Number" = 4,
                     "Proportion" = 4,
                     "Standard Deviation" = 4))
writeLines(kabled_with_weights_descriptive_table, 
           paste0(outputs_path, "weighted_demographics_table.tex"))


dependent_variables <- tibble(variable = "uk_covid_performance_w21_num",
                          neat_variable_name = "UK covid performance") %>%
  add_row(variable = "death_toll_gov_fault_w21",
          neat_variable_name = "Hold gov. responsible for death toll") %>%
  add_row(variable = "vaccine_gov_fault_w21",
          neat_variable_name = "Hold gov. responsible for vaccine") %>%
  add_row(variable = "covid_overall_gov_fault_w21",
          neat_variable_name = "Hold gov. responsible overall") %>%
  add_row(variable = "retro_handle_w21_num",
          neat_variable_name = "Retrospective handling") %>%
  add_row(variable = "retro_handle_change_w21",
          neat_variable_name = "Change in retrospective handling") %>%
  mutate(answered = paste0("ans_", variable))

# Weighted means
with_weights_dep_vars <- bind_rows(map(dependent_variables$variable, 
    weighted_means_dvars)) %>%
  left_join(dependent_variables, by = "variable") %>%
  select(-c(answered, variable)) %>%
  rename(Variable = neat_variable_name) %>%
  relocate(Variable) 
# full path specified here so sed can pick it up too
kabled_with_weights_dep_vars <- kable(with_weights_dep_vars,
    col.names = c("Variable", repeated_names, repeated_names),
    format = "latex", 
    booktabs = TRUE, 
    caption = "Mean and standard deviation of main dependent variables (wave 21 weights applied)",
    label = " weighted_means_dvars",
    linesep = "",
    align = "lcccccccc") %>%
  kable_styling(latex_options = c("hold_position", "scale_down")) %>%
  add_header_above(c(" " = 1,
                     "Mean" = 4,
                     "Standard deviation" = 4))
writeLines(kabled_with_weights_dep_vars, 
           paste0(outputs_path, "weighted_means_dvars.tex"))

missings <- ex %>%
  mutate(ans_uk_covid_performance_w21_num = ifelse(
    is.na(uk_covid_performance_w21_num), 0, 1)) %>%
  mutate(ans_death_toll_gov_fault_w21 = ifelse(
    is.na(death_toll_gov_fault_w21 ), 0, 1)) %>%
  mutate(ans_vaccine_gov_fault_w21 = ifelse(
    is.na(vaccine_gov_fault_w21), 0, 1)) %>%
  mutate(ans_covid_overall_gov_fault_w21 = ifelse(
    is.na(covid_overall_gov_fault_w21), 0, 1)) %>%
  mutate(ans_retro_handle_w21_num = ifelse(
    is.na(retro_handle_w21_num ), 0, 1)) %>%
  mutate(ans_retro_handle_change_w21 = ifelse(
    is.na(retro_handle_change_w21), 0, 1)) 

without_weights_ans_dep_vars <- bind_rows(map(dependent_variables$answered, 
    dvar_dont_knows,
    data = missings)) %>%
  left_join(dependent_variables, by = "answered") %>%
  select(-c(answered, variable)) %>%
  rename(Variable = neat_variable_name) %>%
  relocate(Variable) 

kabled_without_weights_ans_dep_vars <- kable(without_weights_ans_dep_vars,
    col.names = c("Variable", repeated_names, repeated_names),
    format = "latex", 
    booktabs = TRUE, 
    caption = "Mean and standard deviation of main dependent variables (wave 21 weights applied)",
    label = " weighted_means_dvars",
    linesep = "",
    align = "lcccccccc") %>%
  kable_styling(latex_options = c("hold_position", "scale_down")) %>%
  add_header_above(c(" " = 1,
                     "Respondent number" = 4,
                     "Response rate" = 4))
writeLines(kabled_without_weights_ans_dep_vars, 
           paste0(outputs_path, "unweighted_dvar_dont_knows.tex"))

without_weights_min_max <- bind_rows(map(dependent_variables$variable, 
    min_max_dvars)) %>%
  left_join(dependent_variables, by = "variable") %>%
  select(-c(answered, variable)) %>%
  rename(Variable = neat_variable_name) %>%
  relocate(Variable) 

kabled_without_weights_min_max <- kable(without_weights_min_max,
    col.names = c("Variable", repeated_names, repeated_names),
    format = "latex", 
    booktabs = TRUE, 
    caption = "Minimum and maximum values of main dependent variables",
    label = " unweighted_min_max_dvars",
    linesep = "",
    align = "lcccccccc") %>%
  kable_styling(latex_options = c("hold_position", "scale_down")) %>%
  add_header_above(c(" " = 1,
                     "Minimum value" = 4,
                     "Maximum value" = 4))
writeLines(kabled_without_weights_min_max, 
           paste0(outputs_path, "unweighted_min_max_dvars.tex"))

